package com.liwen.service;

import com.liwen.api.enums.Ins;
import com.liwen.service.config.HandlerConfig;
import com.liwen.service.coop.BaseBizService;
import org.springframework.context.ApplicationContext;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

import javax.annotation.Resource;

/**
 * @author: li.wen
 * @time: 2018-04-19 16:00
 * @description: 业务层基础服务
 */
@Service
public class BaseSupport {

    @Resource
    private ApplicationContext context;
    @Resource
    private ShardedJedisPool shardedJedisPool;
    @Resource
    private TaskExecutor taskExecutor;

    /**
     * 获取处理服务
     *
     * @param ins
     * @return
     */
    protected BaseBizService handler(Ins ins) {
        return (BaseBizService) context.getBean(HandlerConfig.getHandler(ins));
    }
    /**
     * 获得bean实例
     *
     * @param name
     * @return
     */
    protected Object getBean(String name) {
        return context.getBean(name);
    }

    /**
     * 获取redis资源
     *
     * @return
     */
    protected ShardedJedis redis() {
        return shardedJedisPool.getResource();
    }

    /**
     * 执行任务
     *
     * @param task
     */
    protected void run(Runnable task) {
        taskExecutor.execute(task);
    }
}
